<?php

class Product extends BaseProduct
{
	public function getSlug()
	{
		return ToolRental::slugify($this->getName());
	}

	public function getImage()
	{
		return "../../../images/products/" . $this->getSlug() . ".jpg";
	}

	public function save(PropelPDO $con = null)
	{
  		$ret = parent::save($con);
 
  		$this->updateLuceneIndex();
 
  		return $ret;
	}

	public function updateLuceneIndex()
	{
	  $index = ProductPeer::getLuceneIndex();
	 
	  // remove existing entries
	  foreach ($index->find('pk:'.$this->getId()) as $hit)
	  {
	    $index->delete($hit->id);
	  }
	 
	  // don't index expired and non-activated jobs
	  //if ($this->isExpired() || !$this->getIsActivated())
	  //{
	  //  return;
	  //}
	 
	  $doc = new Zend_Search_Lucene_Document();
	 
	  // store job primary key to identify it in the search results
	  $doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId()));
	 
	  // index job fields
	  $doc->addField(Zend_Search_Lucene_Field::UnStored('naam', $this->getName(), 'utf-8'));
	  $doc->addField(Zend_Search_Lucene_Field::UnStored('omschrijving', $this->getDescription(), 'utf-8'));
	 
	  // add job to the index
	  $index->addDocument($doc);
	  $index->commit();
	}

	public function delete(PropelPDO $con = null)
	{
	  $index = ProductPeer::getLuceneIndex();
	 
	  foreach ($index->find('pk:'.$this->getId()) as $hit)
	  {
	    $index->delete($hit->id);
	  }
	 
	  return parent::delete($con);
	}

	public static function doDeleteAll($con = null)
	{
	  if (file_exists($index = self::getLuceneIndexFile()))
	  {
	    sfToolkit::clearDirectory($index);
	    rmdir($index);
	  }
	 
	  return parent::doDeleteAll($con);
	}
}
